
package com.amado.springboot.springbootcms.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.amado.springboot.springbootcms.controll.UserController;

/**
 * @author wangyl-910
 * @description
 * @date 2018/3/2
 * @see
 */
public class LoginInterceptor implements HandlerInterceptor {

	private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		LOGGER.debug("all url = {}", request.getRequestURI());
		if (request.getSession().getAttribute("USER_SESSION") == null) {
			LOGGER.debug("url = {}", request.getRequestURI());
			response.sendRedirect(request.getContextPath() + "/login");
		}
		LOGGER.debug("come preHandle");
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		LOGGER.debug("come postHandle");
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		LOGGER.debug("come afterCompletion");
	}
}
